package com.example.administrator.daydayappmarket.game.fragment.fragments.classify.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.example.administrator.daydayappmarket.Contacts;
import com.example.administrator.daydayappmarket.R;
import com.example.administrator.daydayappmarket.game.fragment.fragments.choiceness.app.App;
import com.example.administrator.daydayappmarket.game.fragment.fragments.classify.fragment.activity.GameClassifySecondActivity;
import com.example.administrator.daydayappmarket.game.fragment.fragments.classify.fragment.adapter.GameClassifyGridViewAdapter;
import com.example.administrator.daydayappmarket.game.fragment.fragments.classify.fragment.model.GameClassifyGridViewInfo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * A simple {@link Fragment} subclass.
 */
public class GameClassifyFragment extends Fragment {

    private List<GameClassifyGridViewInfo> data;
    private RequestQueue requestQueue;
    private GameClassifyGridViewAdapter adapter;


    @InjectView(R.id.fragment_game_classify_gridView)
    GridView mFragmentGameClassifyGridView;

    public GameClassifyFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        data=new ArrayList<>();

        requestQueue = App.getApp().getRequestQueue();

        StringRequest stringRequest=new StringRequest(Request.Method.GET, Contacts.URL_GAME_CLASSIFY,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        if(response!=null){

                            try {
                                JSONArray jsonArray=new JSONArray(response);

                                for (int i = 0; i < jsonArray.length(); i++) {

                                    JSONObject jsonObject = jsonArray.getJSONObject(i);

                                    GameClassifyGridViewInfo gameClassifyGridViewInfo=new GameClassifyGridViewInfo();

                                    gameClassifyGridViewInfo.parseJson(jsonObject);

                                    data.add(gameClassifyGridViewInfo);
                                }

                                adapter.notifyDataSetChanged();

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        stringRequest.setTag("request");

        requestQueue.add(stringRequest);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_game_classify, container, false);
        ButterKnife.inject(this, view);

        adapter=new GameClassifyGridViewAdapter(data,getContext());

        mFragmentGameClassifyGridView.setAdapter(adapter);

        mFragmentGameClassifyGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                Intent intent=new Intent(getContext(), GameClassifySecondActivity.class);

               intent.putExtra("id",data.get(i).getId());
                getActivity().startActivity(intent);
            }
        });

        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);

        requestQueue.cancelAll("request");
    }
}
